package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.bus.PathServiceMatcher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;
    @Autowired
    private PathServiceMatcher pathServiceMatcher;

	// 修改声音
	@PutMapping("updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}


	// 修改接口：根据id查询声音
	@GetMapping("getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}




	// 删除声音
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	// 查询当前用户声音分页列表
	@GuiGuLogin(required = true)
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@Parameter(name = "page",description = "当前页面",required = true)
														@PathVariable Long page,
														@Parameter(name = "limit",description = "每页记录数",required = true)
														@PathVariable Long limit,
														@Parameter(name = "trackInfoQuery",description = "查询对象",required = false)
														@RequestBody TrackInfoQuery trackInfoQuery){
		//	设置当前用户Id
		trackInfoQuery.setUserId(AuthContextHolder.getUserId());
		//	创建对象
		Page<TrackListVo> trackListVoPage =  new Page<>(page, limit);
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		//	返回数据
		return Result.ok(trackListVoIPage);
	}


	// 保存声音信息
	@Operation(summary = "新增声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo, AuthContextHolder.getUserId());
		return Result.ok();
	}


	// 上传声音
	@Operation(summary = "上传声音")
	@PostMapping("uploadTrack")
	public Result<Map<String, Object>> uploadTrack(MultipartFile file){
		Map<String, Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

}

